<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/23
 * Time: 15:28
 */

namespace app\api\validate;


use app\lib\exception\ParamterException;

class OrderPlace extends BaseValidate
{
    protected $rule = [
        'products' => 'checkProducts'
    ];

    protected $singleRule = [
        'product_id' => 'require|isPositiveInteger',
        'count' => 'require|isPositiveInteger'
    ];

    /**
     * 对商品列表进行检测
     * @param $values
     * @return bool
     * @throws ParamterException
     */
    protected function checkProducts($values){
        if(empty($values)){
            throw new ParamterException([
                'msg' => '商品列表不能为空'
            ]);
        }
        foreach ($values as $value){
            $this->checkProduct($value);
        }
        return true;
    }

    /**
     * 检测单个商品参数对不对
     * @param $value
     * @throws ParamterException
     */
    protected function checkProduct($value){
        $validate = new BaseValidate($this->singleRule);
        $result = $validate->check($value);
        if(!$result){
            throw new ParamterException([
                'msg' => '商品列表参数错误'
            ]);
        }
    }
}